package com.zx.scp.controller;


import com.zx.map.utils.BaseBaseClass;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.ResponseResult;
import com.zx.map.utils.SUConstants;
import com.zx.platform.utils.StrUtils;
import com.zx.scp.service.IIncomeService;
import com.zx.scp.service.model.YintongPayRequestModel;
import com.zx.scp.service.util.swift.XmlUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by bibei on 2017/3/21.
 * 收款
 */
@Controller
@RequestMapping("/income/swiftpay")
public class SwiftInComeController {
	public Logger logger = Logger.getLogger(SwiftInComeController.class.getSimpleName());
	@Autowired
	@Qualifier("swiftPayIncomeServiceImpl")
	private IIncomeService incomeService;

	/**
	 * 通知支付
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/nofityPay")
	public void nofityPay(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			String resString = XmlUtils.parseRequst(request);
			//resString="<xml><bank_type><![CDATA[ALIPAYACCOUNT]]></bank_type><charset><![CDATA[UTF-8]]></charset><fee_type><![CDATA[CNY]]></fee_type><mch_id><![CDATA[7551000001]]></mch_id><nonce_str><![CDATA[1493964425935]]></nonce_str><openid><![CDATA[2088112245048087]]></openid><out_trade_no><![CDATA[P201705051450783]]></out_trade_no><out_transaction_id><![CDATA[2017050521001004080233672807]]></out_transaction_id><pay_result><![CDATA[0]]></pay_result><result_code><![CDATA[0]]></result_code><sign><![CDATA[44F3AB09D0A369B942D6B576C6D25D08]]></sign><sign_type><![CDATA[MD5]]></sign_type><status><![CDATA[0]]></status><time_end><![CDATA[20170505140705]]></time_end><total_fee><![CDATA[1]]></total_fee><trade_type><![CDATA[pay.alipay.native]]></trade_type><transaction_id><![CDATA[7551000001201705056129279730]]></transaction_id><version><![CDATA[2.0]]></version></xml>";
			logger.debug("威富通通知内容：" + resString);
			if (StrUtils.isNotBlank(resString)) {
				Map<String, String> map = XmlUtils.toMap(resString.getBytes(), "utf-8");
				String status = map.get("status");
				String result_code = map.get("result_code");
				if ("0".equals(status) && "0".equals(result_code) && map.containsKey("sign")) {
					Map responseResult = incomeService.payNotify(map);
					response.getWriter().print(responseResult.get(SUConstants.MSG_KEY));
					return;
				}
			}
			response.getWriter().print("fail");
		} catch (Exception e) {
			logger.debug("51码云订单通知处理异常", e);
			try {
				response.getWriter().print("fail");
			} catch (Exception e1) {
			}
		}
	}

	/**
	 * 威富通获取支付信息
	 *
	 * @param orderResponseDomain
	 * @param request
	 * @return
	 */
	@RequestMapping("/startPay")
	@ResponseBody
	public Map startPay(YintongPayRequestModel orderResponseDomain, HttpServletRequest request) {
		try {
			return incomeService.startPay(request, orderResponseDomain);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("威富通获取支付信息异常", e);
			return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
		}
	}

	/**
	 * 微信公众号支付
	 *
	 * @param orderResponseDomain
	 * @param request
	 * @return
	 */
	@RequestMapping("/winxinJsPay")
	@ResponseBody
	public Map winxinJsPay(YintongPayRequestModel orderResponseDomain, HttpServletRequest request) {
		try {
			return incomeService.winxinJsPay(request, orderResponseDomain);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("威富通获取支付信息异常", e);
			return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
		}
	}
}
